﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using Module.Core.LoginSystem.Commons.Framwork;

namespace Web.Cms.CommentProducts
{
    public partial class LstCommentProducts : AdminUserControl
    {
        #region Khai báo biến
        private int totalRecords;       
        private int cPage = 1;
        DateTime dateTo = DateTime.Now;
        DateTime dateFrom = DateTime.Now;       
        private int productId = 0;       
        #endregion

        #region OnInit
        protected override void OnInit(EventArgs e)
        {
            try
            {
                base.OnInit(e);
               
            }
            catch (Exception ex)
            {
                lblMessage.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region OnPreRender
        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                //page = int.Parse("0" + Request.QueryString["page"]);
                //if (page == 0) page = 1;

                base.OnPreRender(e);
                if (Request.QueryString["productId"] == "" || Request.QueryString["productId"] == null)
                {
                    txtProductId.Text = "0";                    
                }
                else
                {
                    productId = Convert.ToInt32(Request.QueryString["productId"]);
                    txtProductId.Text = productId.ToString();
                }  

                if (Request.QueryString["dateFrom"] == "" || Request.QueryString["dateFrom"] == null)
                {
                    tbx_DateFrom.Text = DateTime.Now.ToString("MM/dd/yyyy");
                    dateFrom = Convert.ToDateTime(tbx_DateFrom.Text);
                }
                else
                {
                    dateFrom = Convert.ToDateTime(Request.QueryString["dateFrom"]);
                    tbx_DateFrom.Text = dateFrom.ToString("MM/dd/yyyy");
                }                    

                if (Request.QueryString["dateTo"] == "" || Request.QueryString["dateTo"] == null)
                {
                    tbx_DateTo.Text = DateTime.Now.ToString("MM/dd/yyyy");
                    dateTo = Convert.ToDateTime(tbx_DateTo.Text);
                }
                else
                {
                    dateTo = Convert.ToDateTime(Request.QueryString["dateTo"]);
                    tbx_DateTo.Text = dateTo.ToString("MM/dd/yyyy");
                }                   
                
                LoadData();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }
        #endregion

        #region Page_Load
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        #endregion

        #region LoadData
        private void LoadData()
        {

            totalRecords = CommentProductsBL.SelectCount(status,productId, dateFrom.ToString(), dateTo.AddDays(+1).ToString());
            lbTotal.Text = "Tổng số bản ghi: " + totalRecords;
            DataTable dt;
            if (Request.QueryString.Get("cpage") != null)
                cPage = ObjConvertUtility.ToInt32(Request.QueryString.Get("cPage"));
            if (cPage == 0) cPage = 1;
            int from = (cPage - 1) * Const.pageSize20 + 1;
            if (from == 0) from =1;
            int to = (cPage) * Const.pageSize20;

            PagingAjax1.totalRecords = totalRecords;
            PagingAjax1.numPerPage = Const.pageSize20;
            PagingAjax1.queryParam = ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] { "productId", "from", "to", "dateFrom", "dateTo", "cPage" },
                                    new string[] {productId.ToString(), from.ToString(), to.ToString(),dateFrom.ToString(),dateTo.ToString(), "" });

            PagingAjax1.defaultQueryParam =
                ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] { "productId", "from", "to" },
                                    new string[] { productId.ToString(),from.ToString(), to.ToString() });

            string order = null;
            dt = CommentProductsBL.Search(status,productId, dateFrom.ToString(), dateTo.AddDays(+1).ToString(), from, to, order);
            if (dt != null && dt.Rows.Count > 0)
            {

                grdCommentProduct.DataSource = dt;
                grdCommentProduct.RowDataBound += new GridViewRowEventHandler(grdCommentProduct_RowDataBound);
                grdCommentProduct.DataBind();

            }
        }
        #endregion

        protected void grdCommentProduct_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "CommandStatus":
                        try
                        {
                            int id = Convert.ToInt32(e.CommandArgument);

                            if (status == 2)// Bài bình luận cuả bạn đang chớ duyệt->Status=0
                            {
                                //Update trạng thái
                                CommentProductsBL.UpdateStatus(id, 1);//Sau khi update Status=1 bài bình luận đã đưa lên web
                            }

                        }
                        catch (Exception ex)
                        {
                            lblMessage.Text = ex.Message;
                        }
                        break;
                    case "Commanddelete":
                        try
                        {
                            int id = Convert.ToInt32(e.CommandArgument);

                            CommentProductsBL.Delete(id);
                        }
                        catch (Exception ex)
                        {
                            lblMessage.Text = ex.Message;
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }


        }

        protected void grdCommentProduct_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton btnStatus = (ImageButton)e.Row.FindControl("btnStatus");
                Label lblStatus = (Label)e.Row.FindControl("lblStatus");
                e.Row.Cells[0].Text = (Const.pageSize20 * (cPage - 1) + e.Row.DataItemIndex + 1).ToString();
                if (status == 2)
                {
                    lblStatus.Text = "Chờ duyệt";
                    btnStatus.ImageUrl = "/App_Themes/Default/Images/Buttons/btn_forward.gif";
                    btnStatus.OnClientClick = "return confirm('Bạn có muốn đăng bài bình luận này không?')";
                }
                else if (status == 1)
                {
                    lblStatus.Text = "Đã duyệt";
                    btnStatus.ImageUrl = "/App_Themes/Default/Images/Buttons/btn_pending.gif";

                }
            }

        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?ctrlkey=" + CurrentCommand.CtrlKey + "&productId=" + txtProductId.Text + "&dateFrom=" + tbx_DateFrom.Text.Trim() + "&dateTo=" + tbx_DateTo.Text.Trim());
        }

        private int status;
        public int Status
        {
            set { status = value; }
            get { return status; }
        }


    }
}